package de.szut.sudoku.gui.menu;

import javax.swing.ButtonGroup;
import javax.swing.JMenu;

import java.awt.event.ItemListener;

import de.szut.sudoku.difficulty.Difficulty;
import de.szut.sudoku.difficulty.DifficultyContainer;
import de.szut.sudoku.gui.MainFrame;
import de.szut.sudoku.main.GameController;

/**
 * SubMenu for the difficulty
 * 
 * @author dqi12huisinga
 * 
 */
@SuppressWarnings("serial")
public class OptionsDifficultySubMenu extends JMenu {
	/**
	 * Generates the sub menu when instantiated
	 */
	public OptionsDifficultySubMenu() {
		// registers the submenu as translatable component
		MainFrame
				.getInstance()
				.getlContainer()
				.addTranslatableComponent("gui.menu.OptionsDifficultySubMenu",
						this);
		DifficultyMenuItem[] difficulties = new DifficultyMenuItem[DifficultyContainer.difficulties.length];
		final ButtonGroup buttonGroup = new ButtonGroup();

		// Generates the various difficulty options
		int index = 0;
		for (Difficulty currentDifficulty : DifficultyContainer.difficulties) {
			difficulties[index] = new DifficultyMenuItem();
			// adds the current difficulty as translatable component
			MainFrame
					.getInstance()
					.getlContainer()
					.addTranslatableComponent(
							"gui.menu.OptionsDifficultySubMenu."
									+ currentDifficulty.getDifficultyName()
											.toLowerCase(), difficulties[index]);
			// sets the difficulty related to the difficultybutton
			difficulties[index].setRelatedDifficulty(currentDifficulty
					.getDifficultyName());
			difficulties[index]
					.addItemListener((ItemListener) new DifficultyListener());
			buttonGroup.add(difficulties[index]);
			// selects the current difficulty if the current difficulty is the
			// selected one
			if (index == GameController.getInstance().getdContainer()
					.getIndexOfCurrentDifficulty()) {
				difficulties[index].setSelected(true);
			}
			add(difficulties[index]);
			index++;
		}
	}
}
